package com.yphone.ui.pay.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yphone.net.IUIDataListener;
import com.yphone.tools.Constants;
import com.yphone.tools.MyLog;
import com.yphone.tools.NiceUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.pay.contract.OrderContract;
import com.yphone.ui.pay.presenter.OrderPresenter;
import com.yphone.utils.ToastUtils;
import com.zzkj.yunphone.R;
import com.yphone.tools.RemoteUtils;
import com.yphone.tools.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class OrderActivity extends AppCompatActivity implements OrderContract.IView {

    private String orderPrice;
    private String priceType;
    private int payNum;
    private TextView tv_order_price;
    private TextView tv_pay_num;
    private TextView tv_pay_type;
    private Button btn_pay;
    private String orderNo;
    private ImageView iv_qrcode;
    private OrderContract.IOrderPresenter orderPresenter;
    private int typeId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();
        initData();
        initListener();
        orderPresenter.checkOrderState(orderNo);
    }

    private void initListener() {
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI"));
                intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
                intent.setFlags(335544320);
                intent.setAction("android.intent.action.VIEW");
                OrderActivity.this.startActivity(intent);
//                OrderActivity.this.finish();
//                EventBus.getDefault().post(new MyEvent.PaySuccessEvent());
//                ToastUtil.showToast(OrderActivity.this, "支付成功");
//                RemoteUtils.canvasStart(OrderActivity.this);
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        orderPrice = intent.getStringExtra("orderPrice");
        priceType = intent.getStringExtra("priceType");
        orderNo = intent.getStringExtra("orderNo");
        typeId = intent.getIntExtra("id",-1);
        payNum = intent.getIntExtra("payNum", -1);
        tv_order_price.setText(orderPrice);
        tv_pay_num.setText(String.valueOf(payNum));
        tv_pay_type.setText(priceType);

        new OrderPresenter(this);
        CommSmallLoadingDialog.showDialog(this,"");
        orderPresenter.getQRCode(typeId+"",orderNo,payNum);
    }

    private void initView() {
        tv_order_price = findViewById(R.id.tv_order_price);
        tv_pay_num = findViewById(R.id.tv_pay_num);
        tv_pay_type = findViewById(R.id.tv_pay_type);
        btn_pay = findViewById(R.id.btn_pay);
        iv_qrcode = findViewById(R.id.iv_qrcode);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        orderPresenter.destory();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void updateQRCode(String img64) {
        MyLog.e("img64 = "+img64);
        iv_qrcode.setImageBitmap(NiceUtil.img64ToBitmap(img64));
    }

    @Override
    public void paySuccess() {
        ToastUtils.showLong("支付成功！");
        MyLog.e("支付成功！");
        EventBus.getDefault().post(new MyEvent.paySuccess());
        finish();
    }

    @Override
    public void getOrderError(String msg) {
        finish();
        ToastUtil.showToast(msg);
    }

    @Override
    public void setPresenter(OrderContract.IOrderPresenter iOrderPresenter) {
        this.orderPresenter = iOrderPresenter;
        orderPresenter.start();
    }
}
